<?php
/**
 * Ngay tao: 
 * Y nghia:  module su ly cac su kien khi quan tri cay tu lieu
 */	
class listxml_treeController extends  Zend_Controller_Action {
	public function auth(){
        $url = Efy_Init_Config::_getCurrentHttpAndHost();
        if (($_SESSION['STAFF_PERMISSTION'] != 'ADMIN_SYSTEM') && ($_SESSION['STAFF_PERMISSTION'] != 'ADMIN_OWNER')){
            $url = $url .'public/error/index/';
            $this->_redirect($url);
        }
    }
	public function init(){
		$this->auth();
		Zend_Loader::loadClass('Listxml_modTree');
		Zend_Loader::loadClass('Zend_Json');	    
		//Load cau hinh thu muc trong file config.ini
        $tempDirApp = Zend_Registry::get('conDirApp');
		$this->_dirApp = $tempDirApp->toArray();
		$this->view->dirApp = $tempDirApp->toArray();
		//Lay duong dan thu muc goc (path directory root)
		$this->view->baseUrl = $this->_request->getBaseUrl() . "/public/";
		if (!$this->_request->isXmlHttpRequest()){
			//Cau hinh cho Zend_layout
			Zend_Layout::startMvc(array(
				    'layoutPath' => $this->_dirApp['layout'],
				    'layout' => 'index'			    
				    ));	
			//Load cac thanh phan cau vao trang layout (index.phtml)
			$response = $this->getResponse();
			//Ky tu dac biet phan tach giua cac phan tu
			$this->view->delimitor 	= "!~~!";	
			//Load cau hinh thu muc trong file config.ini de lay ca hang so dung chung
	        $tempConstPublic = Zend_Registry::get('ConstPublic');
			$this->_ConstPublic = $tempConstPublic->toArray();	
			//Lay cac hang so su dung trong JS public
			$objConfig = new Efy_Init_Config();
			$objConst = new Efy_Init_Const();
			$this->view->JSPublicConst = $objConfig->_setJavaScriptPublicVariable();		
			$this->view->UrlAjax = $objConfig->_setUrlAjax();
			$arrConst = $objConst->_setProjectPublicConst();
			$this->view->arrConst = $arrConst;
			// Load tat ca cac file Js va Css
			$this->view->LoadAllFileJsCss = Efy_Publib_Library::_getAllFileJavaScriptCss(''
				,'efy-js'
				,'mtl/tree.js,print.js,ajaxupload.3.5.js,fg.menu.js,jquery.maskedinput-1.3.js,'
				,','
				,'js')
			.Efy_Publib_Library::_getAllFileJavaScriptCss('','efy-style','print.css,simpleModal.css,fg.menu.css',',','css')
			.Efy_Publib_Library::_getAllFileJavaScriptCss('','efy-style','chosen/chosen.css',',','css')
			.Efy_Publib_Library::_getAllFileJavaScriptCss('','efy-js','chosen/chosen.jquery.js',',','js')
			.Efy_Publib_Library::_getAllFileJavaScriptCss('','efy-js','jsTree/_lib/jquery.cookie.js',',','js')		
			.Efy_Publib_Library::_getAllFileJavaScriptCss('','efy-js','jsTree/_lib/jquery.hotkeys.js',',','js')		
			.Efy_Publib_Library::_getAllFileJavaScriptCss('','efy-js','jsTree/jquery.jstree.js',',','js');			
			//Dinh nghia current modul code
			$this->view->currentModulCode = "ADMIN";
			$this->view->currentModulCodeForLeft = $this->_request->getParam('action');
	        $response->insert('header', $this->view->renderLayout('header.phtml','./application/views/scripts/'));    	//Hien thi header 
			$response->insert('menu', $this->view->renderLayout('menu.phtml','./application/views/scripts/'));    		//Hien thi header 		
	        $response->insert('footer', $this->view->renderLayout('footer.phtml','./application/views/scripts/')); 			
		}else{
			$result = array();
			$result = Efy_Publib_Library::_getAllFileJavaScriptCssArray('','efy-js','mtl/training.js,ajaxupload.3.5.js,fg.menu.js,',',','js',$result);
			$result = Efy_Publib_Library::_getAllFileJavaScriptCssArray('','efy-style','fg.menu.css',',','css',$result);
			$result = Efy_Publib_Library::_getAllFileJavaScriptCssArray('','efy-style','chosen/chosen.css',',','css',$result);
			$result = Efy_Publib_Library::_getAllFileJavaScriptCssArray('','efy-js','chosen/chosen.jquery.js',',','js',$result);
			$result = Efy_Publib_Library::_getAllFileJavaScriptCssArray('','efy-js','jsTree/_lib/jquery.cookie.js',',','js',$result);		
			$result = Efy_Publib_Library::_getAllFileJavaScriptCssArray('','efy-js','jsTree/_lib/jquery.hotkeys.js',',','js',$result);		
			$result = Efy_Publib_Library::_getAllFileJavaScriptCssArray('','efy-js','jsTree/jquery.jstree.js',',','js',$result);			
			$this->view->arrJsCss = Zend_Json::encode($result);	
			$baseurl=$this->_request->getbaseurl();			 
		}
	}	
	/**
	 * @todo: Hien thi cay bai giang
	 * Enter description here ...
	 */
	public function trainingAction(){
		$arrWards = array();	
		$this->view->currentModulCodeForLeft = "training";		
		$objFunction = New Efy_Function_RecordFunctions();		
		$this->view->titleBody = "DANH SÁCH BÀI GIẢNG";	
		$this->view->arrListItem = $objFunction->getAllObjectbyListCode('','DM_SO_HS_TREN_TRANG');
		$this->view->iCurrentPage = 1;
		$this->view->iNumberRecordPerPage = 15;
	}
	public function ajaxstaffAction(){}
	public function getChildNode($idNode,$arrTreeGetAll){
		$arrOutput = array();
		foreach ($arrTreeGetAll as $key => $value) {
			if($value['FK_OBJECT'] == $idNode){
				array_push($arrOutput, array('attr'=>array(
												'id'=>$value['PK_OBJECT']
												,'rel'=>'folder'
											),
											'data'=>$value['TR04'],
											'state'=>'closed'
				));
			}
		}
		return $arrOutput;
	}
	/**
	 * @todo: Load cay bai giang
	 * Enter description here ...
	 */
	public function loadtreeAction(){
		$objTree	= new Listxml_modTree();
		$arrTree = '';
		$sType = $this->_request->getParam('sType',null);
		$fullTextSearch = $this->_request->getParam('searchNodes',null);
		$ownerCode = Efy_Publib_Library::_getItemAttrById($_SESSION['arr_all_staff'],$_SESSION['staff_id'],'ownerCode');
		// echo 'avc'.$ownerCode;die;
		$sStaffId = $_SESSION['staff_id'];
		$arrParameter = array(
			'status'=>'HOAT_DONG'
			,'txtFullTextSearch'=>''
			,'type'=>$sType
			,'sOwner_code'=>$ownerCode
			// ,'sStaffId'=>$sStaffId
		);
		$objTree	= new Listxml_modTree();
		// $arrTreeGetAll = $objTree->mTL_TreeGetAll($arrParameter);
		if(!isset($_SESSION['DATA_TREE_TRAINING'])){
			$objTree	= new Listxml_modTree();
			$_SESSION['DATA_TREE_TRAINING'] = $objTree->mTL_TreeGetAll($arrParameter);
		}
		$arrTreeGetAll = $_SESSION['DATA_TREE_TRAINING'];
		$idNode = $this->_request->getParam('id',null);
		$arrTree = $this->getChildNode($idNode,$arrTreeGetAll);
		if ($fullTextSearch != '')
			$arrTree = $arrTreeGetAll;
		echo json_encode($arrTree);
		$this->getHelper('viewRenderer')->setNoRender();  
	}
	/**
	 * @todo: 
	 * Enter description here ...
	 */
	public function addtrainingAction(){
		if (!$this->_request->isXmlHttpRequest()){
			return $this->_helper->redirector('training');
		}
		$arrStaff = $_SESSION['arr_all_staff'];
		$objFunction = New Efy_Function_RecordFunctions();
		$objTree					= new Listxml_modTree();
		$sType = $this->_request->getParam('sType',null);
		$ownerCode = $_SESSION['OWNER_CODE'];
		// echo $_SESSION['staff_id'];
		//Id nguoi dang nhap
		$sStaffId = $_SESSION['staff_id'];
		//Danh sach tat nodes
		$arrParameter = array(
			'status'=>'HOAT_DONG'
			,'txtFullTextSearch'=>''
			,'type'=>$sType
			,'sOwner_code'=>$ownerCode
			// ,'sStaffId'=>$sStaffId
		);
		$arrTreeGetAll = $objTree->mTL_TreeGetAll($arrParameter);
		$iOrder = sizeof($arrTreeGetAll) + 1;
		//don vi trien khai
		$arrTR09 = Efy_Init_Session::SesGetAllOwner();
		$selectValueTrainingTree = $this->_request->getParam('selectValueTrainingTree','');	
		$act = $this->_request->getParam('act','');
		$arrTreeSingle  = $objTree->mTL_TreeGetSingle($selectValueTrainingTree);
		$parentNode = $arrTreeSingle['C_PARENT_NAME'];
		if ($act == '1'){
			$TR03 = $arrTreeSingle['TR03'];
			$TR04 = $arrTreeSingle['TR04'];
			$iOrder = $arrTreeSingle['TR07']; 
			$TR09 = $arrTreeSingle['TR09'];
			$TR10 = $arrTreeSingle['TR10'];
			$TR05 = $arrTreeSingle['TR05'];
			$TR11 = $arrTreeSingle['TR11'];
		}
		else {
			$tmp = Efy_Publib_Library::_getCode($arrTreeGetAll, $selectValueTrainingTree);
			$TR03 = $arrTreeSingle['TR03'].$tmp;
			$TR04 = '';
			$TR10 = '';
			$TR09 = $_SESSION['OWNER_CODE'];
			$TR05 = '';
			$parentNode = $arrTreeSingle['TR04'];
			$TR11 = '';
		}
		$arrPkStaff = explode(",", $TR11);
		if($TR11 != ''){
			$strStaff = Efy_Publib_Library::_getStaff($arrStaff, $arrPkStaff);
			$strStaff = $TR11.'!@@!'.$strStaff;
		} else $strStaff = '';
		if ($parentNode == '')
			$parentNode = $arrTreeSingle['TR04'];
		//Day du lieu va view
		$this->view->selectValueTrainingTree = $selectValueTrainingTree;
		$this->view->act = $act;
		$this->view->arrTR09 = $arrTR09;
		$this->view->iOrder=$iOrder;
		$this->view->TR03 = $TR03;
		$this->view->TR04 = $TR04;
		$this->view->TR09 = $TR09;
		$this->view->TR10 = $TR10;
		$this->view->sType = $sType;
		$this->view->strStaff = $strStaff;
		$this->view->parentNode = $parentNode;
		$this->view->FkParentNode = $arrTreeSingle['TR02'];
	}
	/**
	 * @todo: Luu lai thong tin doi tuong tren tree
	 * Enter description here ...
	 */
	public function savetrainingAction(){
		$objFunction = New Efy_Function_RecordFunctions();
		$objTree	 = new Listxml_modTree();
		$act = $this->_request->getParam('hdn_act','');
		$v_value_list = $this->_request->getParam('v_value_list','');
		$TRPer = $this->_request->getParam('TRPer','');
		if ($TRPer == 0) $v_value_list = '';
		$selectValueTreeImg = $this->_request->getParam('selectValueTreeImg','');
		$selectValueTrainingTree = $this->_request->getParam('selectValueTrainingTree','');
		$FkParentNode = $this->_request->getParam('hdn_FkParentNode','');
		$sType = $this->_request->getParam('sType',null);
		$sType = 'TRAINING';
		if ($act == '1'){
			$TR01 = $selectValueTrainingTree;
			if ($selectValueTreeImg != '')
				$TR02 =  $selectValueTreeImg;
			else $TR02 = $FkParentNode;
		}
		else{
			$TR01 = '';
			if ($selectValueTreeImg != '')
				$TR02 =  $selectValueTreeImg;
			else $TR02 = $selectValueTrainingTree;
		}
		$arrInput = $this->_request->getParam('action');
		$TR05 = $this->_request->getParam('TR05');
		if ($TR05 == 'on') 
			$TR05 = "HOAT_DONG";
		else $TR05 = "KHONG_HOAT_DONG";
		$arrParameter = array(
			'PK_TREE' 	 	=> $TR01,
			'FK_TREE' 		=> $TR02,
			'C_CODE'		=> $this->_request->getParam('hdn_TR03',''),	
			'C_NAME' 		=> $this->_request->getParam('TR04',''),	
			'C_STATUS' 		=> $TR05,
			'C_TYPE'		=> $sType,
			'C_ORDER'  		=> $this->_request->getParam('TR07',''),
			'C_OWNER_CODE' 	=> $this->_request->getParam('TR09',''),
			'v_value_list'  => $v_value_list, 

		);
		$arrResult = $objTree->mTL_TreeUpdate($arrParameter);
		$ownerCode = Efy_Publib_Library::_getItemAttrById($_SESSION['arr_all_staff'],$_SESSION['staff_id'],'ownerCode');		
		if($arrResult){
			$arrParameter = array(
				'status'=>'HOAT_DONG'
				,'txtFullTextSearch'=>''
				,'type'=>$sType
				,'sOwner_code'=>$ownerCode
			);
			$_SESSION['DATA_TREE_TRAINING'] = $objTree->mTL_TreeGetAll($arrParameter);
			
		}
		die();
	}
	/**
	 * @todo: Xoa node tren tree
	 * Enter description here ...
	 */
	public function deletetrainingAction(){ 
		$objTree	 = new Listxml_modTree();
		$sType = $this->_request->getParam('sType',null);
		$sType = 'TRAINING';
		$ListId = $this->_request->getParam('ListId','');
		$count = strlen($ListId);
		$this->view->ListId=$ListId;
		$sRetError = '';
		if($ListId[$count - 1] == ',')
			$ListId = substr($ListId, 0, -1);
		$delimitorElement = ',';
		$sRetError = $objTree->mTL_TreeDelete($ListId,$delimitorElement);
		$ownerCode = Efy_Publib_Library::_getItemAttrById($_SESSION['arr_all_staff'],$_SESSION['staff_id'],'ownerCode');
		$arrParameter = array(
			'status'=>'HOAT_DONG'
			,'txtFullTextSearch'=>''
			,'type'=>$sType
			,'sOwner_code'=>$ownerCode
		);
		$_SESSION['DATA_TREE_TRAINING'] = $objTree->mTL_TreeGetAll($arrParameter);
		$this->getHelper('viewRenderer')->setNoRender();
		
	}
	/**
	 * @author:TuanPM
	 * @see: 28/12/2012
	 * @todo: Tim kiem node tren tree
	 * Enter description here ...
	 */
	public function searchtrainingAction(){
		$sType = $this->_request->getParam('sType',null);
		$sType = 'TRAINING';
		$arrTree = $_SESSION['TREE_TRAINING'];
		// var_dump($arrTree);
		$fullTextSearch = $this->_request->getParam('fullTextSearch','');
		$arrResult = array();
		for($i = 0; $i < sizeof($arrTree); $i++){
			$distinctString = strtolower($arrTree[$i]['TR04']);
			$distinctString = Efy_Publib_Library ::_Replace_VietNamese($distinctString);
			$stringSearch = strtolower($fullTextSearch);
			$stringSearch = Efy_Publib_Library ::_Replace_VietNamese($stringSearch);
			$pos = strpos($distinctString, $stringSearch);
			if($pos === false){
				echo '';
			}
			else
				array_push($arrResult,$arrTree[$i]['PK_OBJECT']);
		}
		echo json_encode($arrTree);
		$this->getHelper('viewRenderer')->setNoRender();
	}			
}
?>
